package cn.mumzone.pos.util;

import android.content.Intent;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.model.BillConsumeDetailsEntity;
import cn.mumzone.pos.model.BillDetailEntity;
import cn.mumzone.pos.model.SettleItemEntity;
import cn.mumzone.pos.util.print.PrintDiscountEntity;
import cn.mumzone.pos.util.print.PrintEntity;
import cn.mumzone.pos.util.print.PrintPayTypeEntity;
import cn.mumzone.pos.util.print.PrintSettleEntity;

/**
 * Created by qiuzhenhuan on 2018/11/7.
 */

public class BillUtil {

    public static final String PAY_TYPE_WE_CHAT = "0";
    public static final String PAY_TYPE_ALI = "1";
    public static final String PAY_TYPE_BANK = "2";
    public static final String PAY_TYPE_CASH = "3";
    public static final String PAY_TYPE_BALANCE = "4";
    public static final String PAY_TYPE_CARD = "5";
    public static final String PAY_TYPE_BANK_POS = "6";
    public static final String PAY_TYPE_INTEGRAL = "7";


    public static PrintEntity generatePrintEntity(BillDetailEntity billDetailEntity) {

        PrintEntity mPrintEntity = new PrintEntity();
        // 店铺名称
        mPrintEntity.setShopName(AppConstants.TEMP_SHOP_NAME);
        // 流水号
        mPrintEntity.setSerialNo(billDetailEntity.getOrderCode());
        // 收银员
        mPrintEntity.setWaiter(billDetailEntity.getSellerName());
        // 收银机号
        mPrintEntity.setCashId(billDetailEntity.getEquipment());
        //会员id
        mPrintEntity.setBillingUser(billDetailEntity.getBillingUser());
        //会员姓名
        mPrintEntity.setMemberName(billDetailEntity.getMemberName());
        //会员手机号
        mPrintEntity.setMemberPhone(billDetailEntity.getMemberPhone());
        //会员积分
        mPrintEntity.setIntegral(billDetailEntity.getIntegral());
        //会员余额
        mPrintEntity.setCurrentAmount(billDetailEntity.getCurrentAmount());

        // 总价、商品集合
        if ("1".equals(billDetailEntity.getOrderType())) {
            ArrayList<PrintSettleEntity> settleList = new ArrayList<>();
            if (!ListUtil.isListEmpty(billDetailEntity.getGoodsList())) {
                for (int i = 0; i < billDetailEntity.getGoodsList().size(); i++) {
                    BillConsumeDetailsEntity.GoodsListBean goodsListBean = billDetailEntity.getGoodsList().get(i);
                    PrintSettleEntity printSettleEntity = new PrintSettleEntity();
                    printSettleEntity.setGoodsName(goodsListBean.getGoodsName());
                    printSettleEntity.setGoodsPrice(BigDecimalUtil.getNumber(StringUtils.get(goodsListBean.getRealPay(), goodsListBean.getPrice())));
                    int goodsCount = goodsListBean.getGoodsNum() - Integer.parseInt(BigDecimalUtil.getNumber(goodsListBean.getPresentNum()));
                    printSettleEntity.setGoodsCount(StringUtils.get(goodsCount));
                    if (goodsCount > 0) {
                        double goodsOriginPrice = Double.parseDouble(BigDecimalUtil.getNumber(goodsListBean.getRealPay()));
                        printSettleEntity.setTotalPrice(StringUtils.getFormattedMoney(StringUtils.get(goodsCount * goodsOriginPrice)));
                        settleList.add(printSettleEntity);
                    }
                    if (StringUtils.isMoreThenZero(goodsListBean.getPresentNum())) {
                        PrintSettleEntity printPresentEntity = new PrintSettleEntity();
                        printPresentEntity.setGoodsName(String.format("（赠）%s", goodsListBean.getGoodsName()));
                        printPresentEntity.setGoodsCount(StringUtils.get(goodsListBean.getPresentNum()));
                        printPresentEntity.setGoodsPrice("0");
                        printPresentEntity.setTotalPrice("0");
                        settleList.add(printPresentEntity);
                    }
                }
            }
            mPrintEntity.setGoodsList(settleList);
            mPrintEntity.setTotalPrice(getGoodsOrderDiscountSumAmount(billDetailEntity.getGoodsList()));
        } else if ("0".equals(billDetailEntity.getOrderType())) {
            ArrayList<PrintSettleEntity> settleList = new ArrayList<>();
            if (!ListUtil.isListEmpty(billDetailEntity.getList())) {
                for (int i = 0; i < billDetailEntity.getList().size(); i++) {
                    BillConsumeDetailsEntity.ListBean serviceList = billDetailEntity.getList().get(i);
                    PrintSettleEntity printSettleEntity = new PrintSettleEntity();
                    printSettleEntity.setGoodsName(serviceList.getServiceName());
                    printSettleEntity.setGoodsCount("1");
                    printSettleEntity.setGoodsPrice(BigDecimalUtil.getNumber(StringUtils.get(serviceList.getMemberPrice(), serviceList.getPrice())));
                    printSettleEntity.setTotalPrice(BigDecimalUtil.getNumber(StringUtils.get(serviceList.getMemberPrice(), serviceList.getPrice())));
                    settleList.add(printSettleEntity);
                }
            }
            mPrintEntity.setGoodsList(settleList);
            mPrintEntity.setTotalPrice(getServiceOrderDiscountSumAmount(billDetailEntity.getList()));
        }

        // 优惠集合
        ArrayList<PrintDiscountEntity> printDiscountList = new ArrayList<>();
        // 议价折扣
//        PrintDiscountEntity bargainDiscount = new PrintDiscountEntity("议价折扣", billDetailEntity.getBargainingAmount());
//        printDiscountList.add(bargainDiscount);

//        if (!StringUtils.isEmpty(billDetailEntity.getGoodsDiscount())) {
//            PrintDiscountEntity mlDiscount = new PrintDiscountEntity("商品折扣", billDetailEntity.getGoodsDiscount());
//            printDiscountList.add(mlDiscount);
//        }
        // 会员折扣
//        PrintDiscountEntity memberDiscount = new PrintDiscountEntity("会员折扣",
//                TextUtils.isEmpty(billDetailEntity.getMemberDiscount()) ? "0.0" : billDetailEntity.getMemberDiscount());
//        printDiscountList.add(memberDiscount);


//        if (Double.parseDouble(BigDecimalUtil.getNumber(billDetailEntity.getPresentDiscount())) > 0) {
        //赠品优惠
//            PrintDiscountEntity presentDiscount = new PrintDiscountEntity("赠品优惠", billDetailEntity.getPresentDiscount());
//            printDiscountList.add(presentDiscount);
//        }


        if (Double.parseDouble(BigDecimalUtil.getNumber(billDetailEntity.getMlPrice())) > 0) {
            // 抹零折扣
            PrintDiscountEntity mlDiscount = new PrintDiscountEntity("抹零", billDetailEntity.getMlPrice());
            printDiscountList.add(mlDiscount);
        }

        if (!ListUtil.isListEmpty(billDetailEntity.getDiscountList())) {
            for (BillDetailEntity.DiscountItemBean bean : billDetailEntity.getDiscountList()) {
                PrintDiscountEntity tickedDiscount = new PrintDiscountEntity(StringUtils.get(bean.getName()), bean.getAmount());
                printDiscountList.add(tickedDiscount);
            }
        }

        mPrintEntity.setDiscountList(printDiscountList);

        // 支付方式集合
        ArrayList<PrintPayTypeEntity> printPayTypeList = new ArrayList<>();
        String payType = billDetailEntity.getPayType();
        if (!TextUtils.isEmpty(payType)) {
            JSONObject jsonObject = JSONObject.parseObject(payType);
            Set<String> keys = jsonObject.keySet();
            for (String key : keys) {
                if (key.equals("3") && !StringUtils.isEmpty(billDetailEntity.getCashChange())) {
                    printPayTypeList.add(new PrintPayTypeEntity(key, getPayType(key), new BigDecimal(billDetailEntity.getCashCollection()).toPlainString()));
                    if (StringUtils.isNumber(billDetailEntity.getCashChange()) && Double.parseDouble(billDetailEntity.getCashChange()) > 0) {
                        printPayTypeList.add(new PrintPayTypeEntity(key, "找零", new BigDecimal(billDetailEntity.getCashChange()).toPlainString()));
                    }
                } else {
                    printPayTypeList.add(new PrintPayTypeEntity(key, getPayType(key), StringUtils.get(jsonObject.get(key))));
                }
            }
        }
        // 次卡支付需要单独处理，不计入支付方式
        if (ListUtil.isListNotEmpty(billDetailEntity.getTimesCardList())) {
            Double totalAmount = 0.0;
            for (BillDetailEntity.DiscountItemBean timesCard : billDetailEntity.getTimesCardList()) {
                totalAmount += Double.parseDouble(timesCard.getAmount());
            }
            printPayTypeList.add(new PrintPayTypeEntity(PAY_TYPE_CARD, "次卡支付",
                    new BigDecimal(totalAmount).toPlainString()));
        }
        mPrintEntity.setPayTypeList(printPayTypeList);

        return mPrintEntity;
    }


    /**
     * 计算商品订单金额
     *
     * @param list
     * @return
     */
    public static String getGoodsOrderOriginSumAmount(List<BillConsumeDetailsEntity.GoodsListBean> list) {

        if (ListUtil.isListEmpty(list)) {
            return "0.00";
        }

        double totalPrice = 0.0;
        for (BillConsumeDetailsEntity.GoodsListBean listBean : list) {
            String originPrice = listBean.getPrice();
            totalPrice += (Double.parseDouble(originPrice) * listBean.getGoodsNum());

        }
        return String.valueOf(totalPrice);
    }


    /**
     * 计算商品订单金额
     *
     * @param list
     * @return
     */
    public static String getGoodsOrderDiscountSumAmount(List<BillConsumeDetailsEntity.GoodsListBean> list) {

        if (ListUtil.isListEmpty(list)) {
            return "0.00";
        }

        double totalPrice = 0.0;
        for (BillConsumeDetailsEntity.GoodsListBean listBean : list) {
            String originPrice;
            int presentNum = 0;
            if (!StringUtils.isEmpty(listBean.getPresentNum())) {
                presentNum = Integer.parseInt(listBean.getPresentNum());
            }
            if (!listBean.getRealPay().equals(listBean.getPrice())) {

                originPrice = listBean.getRealPay();
            } else {
                originPrice = listBean.getPrice();
            }
            totalPrice += (Double.parseDouble(originPrice) * (listBean.getGoodsNum() - presentNum));
        }
        return String.valueOf(totalPrice);
    }

    /**
     * 获取商品实际支付总价
     *
     * @param list
     * @return
     */
    public static String getGoodsOrderDiscountRealSumAmount(List<BillConsumeDetailsEntity.GoodsListBean> list) {

        if (ListUtil.isListEmpty(list)) {
            return "0.00";
        }

        double totalPrice = 0.0;
        for (BillConsumeDetailsEntity.GoodsListBean listBean : list) {
            String originPrice;
            originPrice = listBean.getRealPay();
            totalPrice += (Double.parseDouble(originPrice) * listBean.getGoodsNum());

        }
        return String.valueOf(totalPrice);
    }

    /**
     * 计算服务订单金额
     *
     * @param list
     * @return
     */
    public static String getServiceOrderOriginSumAmount(List<BillConsumeDetailsEntity.ListBean> list) {

        if (ListUtil.isListEmpty(list)) {
            return "0.00";
        }

        double totalPrice = 0.0;
        for (BillConsumeDetailsEntity.ListBean listBean : list) {

            totalPrice += Double.parseDouble(listBean.getPrice());

        }
        return String.valueOf(totalPrice);
    }

    /**
     * 计算服务订单金额
     *
     * @param list
     * @return
     */
    public static String getServiceOrderDiscountSumAmount(List<BillConsumeDetailsEntity.ListBean> list) {

        if (ListUtil.isListEmpty(list)) {
            return "0.00";
        }

        double totalPrice = 0.0;
        for (BillConsumeDetailsEntity.ListBean listBean : list) {

            String originPrice;
            if (!listBean.getPrice().equals(StringUtils.get(listBean.getMemberPrice(), listBean.getPrice()))) {
                originPrice = listBean.getMemberPrice();
            } else {
                originPrice = listBean.getPrice();
            }

            totalPrice += Double.parseDouble(originPrice);

        }
        return String.valueOf(totalPrice);
    }

    /**
     * 计算服务订单实际支付金额
     *
     * @param list
     * @return
     */
    public static String getServiceOrderDiscountRealSumAmount(List<BillConsumeDetailsEntity.ListBean> list) {

        if (ListUtil.isListEmpty(list)) {
            return "0.00";
        }

        double totalPrice = 0.0;
        for (BillConsumeDetailsEntity.ListBean listBean : list) {

            String originPrice;
            if (!listBean.getPrice().equals(StringUtils.get(listBean.getMemberPrice(), listBean.getPrice()))) {
                originPrice = listBean.getMemberPrice();
            } else {
                originPrice = listBean.getPrice();
            }

            totalPrice += Double.parseDouble(originPrice);

        }
        return String.valueOf(totalPrice);
    }

    /**
     * 获取支付方式名称
     *
     * @param key
     * @return
     */
    private static String getPayType(String key) {
        switch (key) {
            case PAY_TYPE_WE_CHAT:
                return "微信支付";
            case PAY_TYPE_ALI:
                return "支付宝支付";
            case PAY_TYPE_BANK:
                return "银行卡支付";
            case PAY_TYPE_CASH:
                return "现金支付";
            case PAY_TYPE_BALANCE:
                return "会员余额支付";
            case PAY_TYPE_CARD:
                return "次卡支付";
            case PAY_TYPE_INTEGRAL:
                return "积分抵现";
        }
        return "未知";
    }

    /**
     * 获取支付方式名称
     *
     * @param key
     * @return
     */
    public static String getSimplePayType(String key) {
        switch (key) {
            case PAY_TYPE_WE_CHAT:
                return "微信";
            case PAY_TYPE_ALI:
                return "支付宝";
            case PAY_TYPE_BANK:
                return "银行卡";
            case PAY_TYPE_CASH:
                return "现金";
            case PAY_TYPE_BALANCE:
                return "会员余额";
            case PAY_TYPE_CARD:
                return "次卡";
            case PAY_TYPE_INTEGRAL:
                return "积分抵现";
            case "6":
                return "总计";
        }
        return "未知";
    }

}
